package zhihui.mobile.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.zgm.utils.UiUtils;
import com.zgm.utils.ui.LoadingDialog;

import java.util.HashSet;
import java.util.Set;

import zhihui.mobile.R;
import zhihui.mobile.app.MyApplication;
import zhihui.mobile.presenter.IPresenter;

/**
 * Created by 张公铭 on 2017/7/5 0005.
 * Fragment 的基类，所有的Fragment 需要继承该类
 */

public abstract class BaseFragment extends Fragment implements IView
{

    protected LoadingDialog mLoadingDialog;
    private Set<IPresenter> mAllPresenters = new HashSet<IPresenter>();
    protected View mView;

    /**
     * 获取布局文件ID
     */
    protected abstract int getLayoutResId();

    /**
     * 获取子类的Persenter,一个activity 可能会有多个persenter
     */
    protected abstract IPresenter[] getPresenters();

    /**
     * 初始化presenter
     */
    protected abstract void onInitPresenter();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
    {
        mView = inflater.inflate(getLayoutResId(), container, false);
        addPresenters();
        onInitPresenter();
        initView();
        return mView;
    }

    protected void addPresenters()
    {
        IPresenter[] presenters = getPresenters();
        if (presenters != null)
        {
            for (int i = 0; i < presenters.length; i++)
            {
                mAllPresenters.add(presenters[i]);
            }
        }
    }

    @Override
    public void goBack(Class cls)
    {
        destroyLoading();
        MyApplication.finishActivity(cls);
    }

    @Override
    public void showLoading()
    {
        if (mLoadingDialog == null)
        {
            mLoadingDialog = new LoadingDialog(getActivity(), R.drawable.loading_image);
        }
        mLoadingDialog.dismiss();
        mLoadingDialog.show();
    }

    @Override
    public void hindLoading()
    {
        if (mLoadingDialog != null)
        {
            mLoadingDialog.dismiss();
        }
    }

    @Override
    public void destroyLoading()
    {
        if (mLoadingDialog != null)
        {
            mLoadingDialog.cancel();
            mLoadingDialog = null;
        }
    }

    @Override
    public void showMessage(String message)
    {
        UiUtils.ToastInCenter(getActivity(), message, Toast.LENGTH_SHORT);
    }

    @Override
    public void onResume()
    {
        super.onResume();
        for (IPresenter presenter : mAllPresenters)
        {
            if (presenter != null)
            {
                presenter.onResume();
            }
        }
    }

    @Override
    public void onStart()
    {
        super.onStart();
        for (IPresenter presenter : mAllPresenters)
        {
            if (presenter != null)
            {
                presenter.onStart();
            }
        }
    }

    @Override
    public void onPause()
    {
        super.onPause();
        for (IPresenter presenter : mAllPresenters)
        {
            if (presenter != null)
            {
                presenter.onPause();
            }
        }
    }

    @Override
    public void onStop()
    {
        super.onStop();
        for (IPresenter presenter : mAllPresenters)
        {
            if (presenter != null)
            {
                presenter.onStop();
            }
        }
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
        for (IPresenter presenter : mAllPresenters)
        {
            if (presenter != null)
            {
                presenter.onDestroy();
            }
        }
    }
}
